<template>
  <view class="uni-swiper-container">
    <!-- 轮播图主体 -->
    <swiper
        class="swiper"
        :current="currentIndex"
        :indicator-dots="true"
        :autoplay="autoplay"
        :interval="interval"
        :duration="duration"
        :circular="circular"
        indicator-color="rgba(255, 255, 255, 0.5)"
        indicator-active-color="#ffffff"
        @change="onSwiperChange"
    >
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <view class="swiper-item-wrapper">
          <image :src="item.avatar" mode="aspectFill" @click="handleClick(item)"></image>

          <!-- 可选：图片上的标题 -->
<!--          <view class="swiper-title" v-if="item.title">{{ item.title }}</view>-->
        </view>
      </swiper-item>
    </swiper>

<!--    &lt;!&ndash; 自定义指示器 &ndash;&gt;-->
<!--    <view class="indicator-container" v-if="showIndicator">-->
<!--      <view-->
<!--          class="indicator-dot"-->
<!--          :class="{ active: index === currentIndex }"-->
<!--          v-for="(item, index) in swiperList"-->
<!--          :key="index"-->
<!--      ></view>-->
<!--    </view>-->
  </view>
</template>

<script>
export default {
  name: 'UniSwiper',
  props: {
    // 轮播图数据
    swiperList: {
      type: Array,
      default: () => []
    },
    // 是否自动播放
    autoplay: {
      type: Boolean,
      default: true
    },
    // 自动切换时间间隔（ms）
    interval: {
      type: Number,
      default: 3000
    },
    // 滑动动画时长（ms）
    duration: {
      type: Number,
      default: 500
    },
    // 是否循环播放
    circular: {
      type: Boolean,
      default: true
    },
    // 是否显示指示器
    showIndicator: {
      type: Boolean,
      default: true
    },
    // 初始索引
    initialIndex: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      currentIndex: this.initialIndex
    };
  },
  methods: {
    // 轮播图切换事件
    onSwiperChange(e) {
      this.currentIndex = e.detail.current;
      this.$emit('change', this.currentIndex);
    },

    // 点击轮播图项
    handleClick(item) {
      uni.navigateTo({
        url: `/pages/goods/lecturer/introductory?item=${JSON.stringify(item)}`
      })
    }
  },
  watch: {
    // 监听初始索引变化
    initialIndex(newVal) {
      this.currentIndex = newVal;
    }
  }
};
</script>

<style lang="scss" scoped>
@function rem($px) {
  @return ($px / 375) * 100rem;
}

.uni-swiper-container {
  position: relative;
  width: 100%;
  height: 500rpx; // 默认高度，可通过样式覆盖

  .swiper {
    width: 100%;
    height: 100%;

    .swiper-item-wrapper {
      position: relative;
      width: 100%;
      height: 100%;

      image {
        width: 100%;
        height: 100%;
      }

      .swiper-title {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        padding: rem(10) rem(15);
        background-color: rgba(0, 0, 0, 0.6);
        color: white;
        font-size: rem(14);
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
    }
  }

  // 自定义指示器
  .indicator-container {
    position: absolute;
    bottom: rem(10);
    left: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .indicator-dot {
      width: rem(8);
      height: rem(8);
      border-radius: 50%;
      background-color: rgba(255, 255, 255, 0.5);
      margin: 0 rem(4);
      transition: all 0.3s;

      &.active {
        width: rem(20);
        border-radius: rem(4);
        background-color: #fff;
      }
    }
  }
}
</style>